package cn.hesion.leetcode;
/**
 * ClassName: TwoSum <br/>
 * Description: <br/>
 * date: 2022/5/14 20:35<br/>
 *
 * @author Hesion<br />
 * @version
 * @since JDK 1.8
 */

import java.util.Arrays;

/**
 * @author: hesion
 * @create: 2022-05-14 20:35
 **/
public class TwoSum {
    public int[] twoSum(int[] numbers, int target) {
        int n = numbers.length;
        int left = 0, right = n - 1;
        while (true) {
            int sum = numbers[left] + numbers[right];
            if (sum == target) {
                return new int[]{
                        left + 1, right + 1
                };
            } else if (sum < target) {
                ++left;
            }else {
                --right;
            }
        }
    }
    public int[] twoSumHalfSearch(int[] numbers, int target) {
        int n = numbers.length;
        for (int i = 0; i < n; ++i) {
            int low=i+1,high=n-1;
            while (low<=high){
                int mid =(high-low)/2+low;
                if(numbers[mid] == target-numbers[i]){
                    return new int[]{i+1,mid+1};
                }else if(numbers[mid] > target-numbers[i]){
                    high = mid-1;
                }else {
                    low = mid+1;
                }
            }
        }
        return new int[]{-1,-1};
    }

    public static void main(String[] args) {
        int[] ints = new TwoSum().twoSumHalfSearch(new int[]{2, 7, 11, 15}, 9);
        System.out.println(Arrays.toString(ints));
    }
}
